#include "CPlayer.h"

const std::map<std::string, Animation> CPlayer::animations = CPlayer::getAnimations();
const std::map<std::string, Animation> CPlayer::getAnimations()
{
    std::map<std::string, Animation> anims;
    anims["walkLeft"] = Animation("walkLeft", ANIMATION_FLAG_NONE, 8, 0);
    anims["standLeft"] = Animation("standLeft", ANIMATION_FLAG_NONE, 0, 0);
    anims["walkRight"] = Animation("walkRight", ANIMATION_FLAG_NONE, 8, 1);
    anims["standRight"] = Animation("standRight", ANIMATION_FLAG_NONE, 0, 1);
    anims["die"] = Animation("die", ANIMATION_FLAG_ONE_TIME, 3, 1);
    return anims;
}

CPlayer::CPlayer()
{
    CEntity::Type = ENTITY_TYPE_PLAYER;
    animExecutor.SetAnimation(animations.at("standRight"), ANIM_NORMAL);
	MoveLeft = false; // KNOW YOUR PLACE
    MoveRight = false;
    physicalImmuneTimer = NULL;
    health = 50;
}
 
bool CPlayer::OnLoad(char* File, int Width, int Height)
{
    if(CEntity::OnLoad(File, Width, Height) == false)
    {
        return false;
    }
    return true;
}

Acceleration CPlayer::UpdateAcceleration()
{
	Acceleration accel = CEntity::UpdateAcceleration();

	float xAccel = accel.getX();
	float yAccel = accel.getY();
	if(MoveLeft)
    {
        xAccel += -0.5;
    }
    else if(MoveRight)
    {
        xAccel += 0.5;
	}
	else
	{
		if(SpeedX > 0)
		{
			xAccel += -1;
		}
		if(SpeedX < 0)
		{
			xAccel += 1;
		}
	}

	return Acceleration(xAccel, yAccel);
}
 
void CPlayer::OnLoop()
{
    checkTimers();

	if((SpeedX < 0.5f && SpeedX > -0.5f) && !(MoveRight || MoveLeft))
    {
        SpeedX = 0;
    }

    CEntity::OnLoop();
}
 
void CPlayer::OnRender(SDL_Surface* Surf_Display)
{
    CEntity::OnRender(Surf_Display);
}
 
void CPlayer::OnCleanup()
{
    CEntity::OnCleanup();
}
 
void CPlayer::OnAnimate()
{
    if(!FacingRight)
    {
        if(SpeedX != 0)
        {
            if(!(animExecutor.getAnimationName() == "walkLeft"))
            {
                animExecutor.SetAnimation(animations.at("walkLeft"), ANIM_NORMAL);
            }
        }
        else
        {
            if(!(animExecutor.getAnimationName() == "standLeft"))
            {        
                animExecutor.SetAnimation(animations.at("standLeft"), ANIM_NORMAL);
            }
        }
    }
    
    else 
    {
        if(SpeedX != 0)
        {
            if(!(animExecutor.getAnimationName() == "walkRight"))
            {
                animExecutor.SetAnimation(animations.at("walkRight"), ANIM_NORMAL);
            }
        }
        else
        {
            if(!(animExecutor.getAnimationName() == "standRight"))
            {                
                animExecutor.SetAnimation(animations.at("standRight"), ANIM_NORMAL);
            }
        }
    }
 
    CEntity::OnAnimate();
}
 
void CPlayer::OnCollision(CEntity* Entity)
{
    if(Entity->Type == ENTITY_TYPE_ENEMY)
    {
        if((Y + Height) < (Entity->Y + Entity->Height/2)) // Bounce off their head
        {
            SpeedY = -MaxSpeedY;
        }
        else // Player is hit
        {
            if (Flags & ENTITY_FLAG_PHYSICAL_IMMUNE)
            {
                return;
            }
            std::cout << "Health is " << health << std::endl;
            if (--health == 0)
            {
                X = 50;
                Y = 0;
                health = 50;
                return;
            }
            if ((X + Width/2) < (Entity->X + Entity->Width/2)) // Bounce back
            {
                SpeedY = -MaxSpeedY/2;
                SpeedX = -MaxSpeedX/2;
            }
            else
            {
                SpeedY = -MaxSpeedY/2;
                SpeedX = MaxSpeedX/2;
            }
            //Become immune to physical for a time
            Flags = Flags | ENTITY_FLAG_PHYSICAL_IMMUNE;
            if (physicalImmuneTimer != NULL)
            {
                delete physicalImmuneTimer;
            }
            physicalImmuneTimer = new SDLTimer(PHYSICAL_IMMUNE_TIME);
        }
    }
}

void CPlayer::checkTimers()
{
    if (physicalImmuneTimer != NULL && physicalImmuneTimer->isExpired())
    {
        Flags = Flags ^ ENTITY_FLAG_PHYSICAL_IMMUNE;
        delete physicalImmuneTimer;
        physicalImmuneTimer = NULL;
    }
}